package com.relife.core.socket;

import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.log4j.Logger;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

public class MisWebSocketHandler implements WebSocketHandler {
	private static final Logger log = Logger.getLogger("MisWebSocketHandler");

	@Override
	public void afterConnectionClosed(WebSocketSession arg0, CloseStatus arg1) throws Exception {
		log.info("");
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession arg0) throws Exception {
		log.info("");

	}

	@Override
	public void handleMessage(WebSocketSession session, WebSocketMessage<?> arg1) throws Exception {
		log.info(URLDecoder.decode(arg1.getPayload().toString(), "UTF-8"));
		session.sendMessage(new TextMessage(URLEncoder.encode("你给服务器发送了【" + arg1.getPayload().toString() + "】", "UTF-8")));

	}

	@Override
	public void handleTransportError(WebSocketSession arg0, Throwable arg1) throws Exception {
		log.info("");
	}

	@Override
	public boolean supportsPartialMessages() {
		log.info("");
		return true;
	}

}
